package com.ruoyi.base.service.impl;

import com.ruoyi.base.domain.Materials;
import com.ruoyi.base.domain.MaterialsSuppliers;
import com.ruoyi.base.domain.vo.MaterialsVo;
import com.ruoyi.base.mapper.MaterialsMapper;
import com.ruoyi.base.mapper.MaterialsSuppliersMapper;
import com.ruoyi.base.service.IMaterialsService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SeqNumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 物料管理Service业务层处理
 *
 * @author YwaiX
 * @date 2024-10-25
 */
@Service
public class MaterialsServiceImpl implements IMaterialsService {
    @Autowired
    private MaterialsMapper materialsMapper;

    @Autowired
    private MaterialsSuppliersMapper materialsSuppliersMapper;

    /**
     * 查询物料管理
     *
     * @param id 物料管理主键
     * @return 物料管理
     */
    @Override
    public Materials selectMaterialsById(Long id) {
        return materialsMapper.selectMaterialsById(id);
    }

    /**
     * 查询物料管理列表
     *
     * @param materials 物料管理
     * @return 物料管理
     */
    @Override
    public List<MaterialsVo> selectMaterialsList(MaterialsVo materials) {
        return materialsMapper.selectMaterialsList(materials);
    }

    @Override
    public List<Materials> selectMaterials(Materials materials) {
        return materialsMapper.selectMaterials(materials);
    }

    /**
     * 新增物料管理
     *
     * @param materials 物料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMaterials(MaterialsVo materials) {
        Materials material = new Materials();
        material.setMaterialNo(SeqNumberUtils.getWLNumber());
        material.setMaterialName(materials.getMaterialName());
        material.setSpecification(materials.getSpecification());
        material.setGetMode(materials.getGetMode());
        material.setCheckRatio(materials.getCheckRatio());
        material.setReorderPoint(materials.getReorderPoint());
        material.setUnitId(materials.getUnitId());
        material.setStatus(materials.getStatus());
        material.setCreateTime(DateUtils.getNowDate());
        material.setCreateBy(materials.getCreateBy());
        materialsMapper.insertMaterials(material);

        MaterialsSuppliers materialsSuppliers = new MaterialsSuppliers();
        materialsSuppliers.setMaterialId(material.getId().intValue());
        materialsSuppliers.setSupplierId(materials.getSupplierId());
        materialsSuppliers.setPrice(materials.getPrice());

        return materialsSuppliersMapper.insert(materialsSuppliers);
    }

    /**
     * 修改物料管理
     *
     * @param materials 物料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMaterials(MaterialsVo materials) {
        Materials material = new Materials();
        material.setId(materials.getId());
        material.setMaterialNo(materials.getMaterialNo());
        material.setMaterialName(materials.getMaterialName());
        material.setSpecification(materials.getSpecification());
        material.setGetMode(materials.getGetMode());
        material.setCheckRatio(materials.getCheckRatio());
        material.setReorderPoint(materials.getReorderPoint());
        material.setUnitId(materials.getUnitId());
        material.setStatus(materials.getStatus());
        material.setUpdateTime(DateUtils.getNowDate());
        material.setUpdateBy(materials.getCreateBy());
        materialsMapper.updateMaterials(material);

        MaterialsSuppliers materialsSuppliers = new MaterialsSuppliers();
        materialsSuppliers.setMaterialId(materials.getId().intValue());
        materialsSuppliers.setSupplierId(materials.getSupplierId());
        materialsSuppliers.setPrice(materials.getPrice());
        materialsSuppliersMapper.deleteByMaterialIdAndSupplierId(materialsSuppliers);
        return materialsSuppliersMapper.insert(materialsSuppliers);
    }

    /**
     * 批量删除物料管理
     *
     * @param ids 需要删除的物料管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialsByIds(Long[] ids) {
        //materialsSuppliersMapper.deleteByMaterialIdAndSupplierIds(ids);
        return materialsMapper.deleteMaterialsByIds(ids);
    }

    /**
     * 删除物料管理信息
     *
     * @param id 物料管理主键
     * @return 结果
     */
    @Override
    public int deleteMaterialsById(Long id) {
        return materialsMapper.deleteMaterialsById(id);
    }

    @Override
    public int revokeDeleteDelMaterials(Long ids) {
        return materialsMapper.revokeDeleteDelMaterials(ids);
    }
}
